﻿using System;
using System.Collections;
using System.Web.UI;
using System.Web;
using System.Text.RegularExpressions;

namespace CLZ.Common
{
    public class ExcelExport
    {
        System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="dataSource">数据源</param>
        /// <param name="field">导出字段集合</param>
        public void Export(IEnumerable dataSource, string[,] field)
        {
            Export(dataSource, field, "");
        }

        /// <summary>
        /// 导出Excel
        /// </summary>
        /// <param name="dataSource">数据源</param>
        /// <param name="field">导出字段集合</param>
        /// <param name="fileName">文件名</param>
        public   void  Export(IEnumerable dataSource, string[,] field,string  fileName)
        {
            if(string.IsNullOrEmpty(fileName))
            {
                fileName = "Data";
            }
            //System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
            //response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpContext.Current.Server.HtmlEncode(fileName) + ".xls");
            response.ContentEncoding = System.Text.Encoding.Default;
            response.ContentType = "application/vnd.ms-excel;";
            //Response.ContentType = "application/octet-stream;charset=UTF-8";

            if (Regex.IsMatch(HttpContext.Current.Request.Browser.Browser, "^ie", RegexOptions.IgnoreCase))
                response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)+".xls");
            else
                response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);

            response.Write("<table border='1px'>");
            try
            {
                WriteTitle(field);
                WriteContent(dataSource, field);
            }
            catch (Exception)
            {
                throw new Exception("导出错误！");
            }
            response.Write("</table>");
            //response.Write(HttpContext.Current.Server.HtmlEncode(exportContent.ToString()));
            response.End();
        }

        /// <summary>
        /// 输出字段名称
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        private  void WriteTitle(string[,] field)
        {
            response.Write("<tr align='center' style='height:35px'>");
            response.Write("<td>序号</td>");
            for (int i = 0; i < (field.Length/field.Rank); i++)
            {
                response.Write(string.Format("<td>{0}</td>", field[i, 0]));
            }
            response.Write("</tr>");
        }

        /// <summary>
        /// 输出内容
        /// </summary>
        /// <param name="dataSource"></param>
        /// <param name="field"></param>
        /// <returns></returns>
        private void WriteContent(IEnumerable dataSource, string[,] field)
        {
            IEnumerator data = dataSource.GetEnumerator();
            int index = 0;
            object temp;
            while (data.MoveNext())
            {
                index++;
                response.Write("<tr>");
                response.Write(string.Format("<td align='left'>{0}</td>", index.ToString()));
                for (int j = 0; j < (field.Length/field.Rank); j++)
                {
                    temp = GetPropertyValue(field[j, 1],data.Current);
                    response.Write(string.Format("<td align='left'>{0}</td>", temp));
                }
                response.Write("</tr>");
            }
        }

        /// <summary>
        /// 获取字段的值
        /// </summary>
        /// <param name="propertyName">字段名</param>
        /// <param name="row">当前行</param>
        /// <returns>返回的值</returns>
        protected virtual object GetPropertyValue(string propertyName,object  row)
        {
            return DataBinder.GetPropertyValue(row, propertyName);
        }

    }
}
